package com.lagou.controller;

import com.lagou.pojo.Result;
import com.lagou.service.MqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class mqController {
    @Autowired
    MqService mqService;

    @GetMapping("/create")
    public String createOrder(Map<String,Object> map) {
        String orderId = UUID.randomUUID().toString();
        Result result = mqService.createOrder(orderId);
        map.put("msg", result.getMsg());
        if (result.getCode() == 0) {
            mqService.publishExpireMsg(orderId);
            map.put("orderId", orderId);
            return "pay";
        } else {
            return "error";
        }
    }

    @GetMapping("/pay/{orderId}")
    public String payOrder(Map<String,Object> map, @PathVariable String orderId) {
        Result result = mqService.payOrder(orderId);
        map.put("msg", result.getMsg());
        if (result.getCode() == 0) {
            map.put("orderId", orderId);
            return "complete";
        } else {
            List<String> expiredOrder = mqService.findHistoryExpiredOrder();
            map.put("expiredOrder", expiredOrder);
            return "history";
        }
    }

    @GetMapping("/")
    public String index() {
        return "index";
    }
}
